<?php

namespace app\superadmin\controller;

use think\Controller;

use think\Request;

use think\Session;

use think\Db;

class Login extends Controller{

    public function index(){

        

    return $this->fetch('login/index');

 }



 	 public function yanzheng(Request $request){

        $where['account'] =  $request->param('username');

        $where['password'] = md5($request->param('pswd'));

        $res = Db::table("yup_admin")->where($where)->find();

        if(!$res){

            //用户名不存在
            return 2;

        }else if($res['is_disable'] != 0){
            return 3;
        }else{
            Session::set("adminid",$res['id']);
            return 1;

        }

    }





 //删除session

    public function bey(Request $request){

       // 接收session值

        $session = $request->session("sname");

        //halt($session);

        // 判断

        if ($session) {

            // 清除session中的所有数据

            Session::clear();

            // 重定向

           return $this->fetch('login/index');



        }else{

            // 重定向

           return $this->fetch('login/index');

        }

    }



    public function doedit(){   

        $adminid = session('adminid');
        $oldpass = input('oldpass', '');
        $pass = input('pass', '');
        $repass = input('repass', '');
        
        if ($adminid) {
            if ($oldpass && $pass && $repass) {
                $admin =  Db::table("yup_admin")->where('id',$adminid)->find();
                if(md5($oldpass) == $admin['password']){
                    if($pass == $repass){
                        Db::table('yup_admin')->where('id',$adminid)->update(['password'=>md5($pass)]);
                        //修改成功
                        return 3;
                    }else{
                        //两次密码不一致
                        return 2;
                    }
                }else{
                    //原密码错误
                    return 1;
                }
            }else{
                // 必填项不能为空
                return 4;
            }
        }else{
            //系统错误，请稍后重试
            return 0;
        }
    }





}